from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from .models import UserProfile


# 扩展Django默认User的Admin
class UserProfileInline(admin.StackedInline):
    """用户扩展信息内联"""
    model = UserProfile
    can_delete = False
    verbose_name_plural = '用户扩展信息'
    
    fieldsets = (
        ('基本信息', {
            'fields': ('nickname', 'avatar', 'phone', 'school', 'major', 'grade', 'student_id')
        }),
        ('状态信息', {
            'fields': ('is_verified', 'credit_score', 'volunteer_hours')
        }),
        ('详细信息', {
            'fields': ('bio', 'preferences', 'favorite_categories', 'reading_interests'),
            'classes': ('collapse',)
        }),
    )


class CustomUserAdmin(BaseUserAdmin):
    """扩展用户管理"""
    inlines = (UserProfileInline,)
    list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff', 'date_joined')


# 重新注册User
admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)


@admin.register(UserProfile)
class UserProfileAdmin(admin.ModelAdmin):
    """用户扩展信息管理"""
    
    list_display = (
        'id', 'user', 'school', 'major', 'is_verified', 
        'credit_score', 'created_at'
    )
    list_filter = ('is_verified', 'school', 'major')
    search_fields = ('user__username', 'user__email', 'nickname', 'phone', 'student_id')
    ordering = ('-created_at',)
    
    fieldsets = (
        ('用户信息', {
            'fields': ('user',)
        }),
        ('基本信息', {
            'fields': ('nickname', 'avatar', 'phone', 'school', 'major', 'grade', 'student_id')
        }),
        ('状态信息', {
            'fields': ('is_verified', 'credit_score', 'volunteer_hours')
        }),
        ('详细信息', {
            'fields': ('bio', 'preferences', 'favorite_categories', 'reading_interests')
        }),
    )
    
    readonly_fields = ('created_at', 'updated_at')
    list_per_page = 20
